/**
 * @description 消息管理
 */

import request from '@/utils/request'
import logger from '@/utils/logger'

/**
 * @description 获取未读消息列表 (分页)
 * @param {object} data { queryFields: {}, currentPage: number, pageSize: number }
 * @returns {Promise}
 */
export async function fetchUnreadMessages(data) {
  try {
    const response = await request({
      url: '/dealer/pzMsgdataTemp/getPzMsgdataTempPagesForNotRead',
      method: 'post',
      data,
    })
    return response
  } catch (error) {
    logger.error('获取未读消息列表失败', error)
    throw error
  }
}

/**
 * @description 获取所有消息列表 (分页)
 * @param {object} data { queryFields: {}, currentPage: number, pageSize: number }
 * @returns {Promise}
 */
export async function fetchAllMessages(data) {
  try {
    const response = await request({
      url: '/dealer/pzMsgdataTemp/getPzMsgdataTempPagesForSelf',
      method: 'post',
      data,
    })
    return response
  } catch (error) {
    logger.error('获取所有消息列表失败', error)
    throw error
  }
}

/**
 * @description 设置单条消息为已读
 * @param {string} msgdataTempId 消息ID
 * @returns {Promise}
 */
export async function postSetMessageRead(msgdataTempId) {
  try {
    const response = await request({
      url: `/dealer/pzMsgdataTemp/updatePzMsgdataTempSetIsRead?msgdataTempId=${msgdataTempId}`,
      method: 'post',
    })
    return response
  } catch (error) {
    logger.error('设置消息已读失败', error)
    throw error
  }
}

/**
 * @description 批量设置消息为已读
 * @param {object} data
 * @returns {Promise}
 */
export async function postSetMessagesReadBatch(data) {
  try {
    const response = await request({
      url: '/dealer/pzMsgdataTemp/updatePzMsgdataTempSetIsRead',
      method: 'post',
      data,
    })
    return response
  } catch (error) {
    logger.error('批量设置消息已读失败', error)
    throw error
  }
}
